<?php

namespace App\Services\Assess;

use App\Models\Assess\VisitorsRecordModel;
use App\Models\Register\ActivateUserManagementInfoModel;
use App\Models\Register\ActivateUserManagementModel;
use App\Services\BaseService;
use App\Services\Common\CommonService;

class ActivateExceptionService extends BaseService
{

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new ActivateUserManagementModel();
    }


    /**
     * @Notes:列表获取数据
     * @Interface getList
     * @return array
     * @author: wuyu
     * @Time: 2023/5/5   10:12
     */
    public function getList()
    {
        $param = request()->all();
        // 查询条件
        $map[] = ['mark','=',1];
        // 搜索字段
        $progressActivate = getter($param, 'progress_activate', 1000);
        $searchField = getter($param, 'search_field', '');
        $searchValue = getter($param, 'search_value', '');
        $asin = getter($param, 'asin', '');
        $page_size = getter($param, 'page', 1);
        $limit = getter($param, 'limit', 10);
        if (!empty($searchField) && !empty($searchValue)) {
            $searchValue = str_replace(['；', ',', '，', "\r\n", "\r", "\n"], ';', $searchValue);
            $searchValueArr = explode(';', $searchValue);
            if ($searchField == 'remark') {
                $map[] = [$searchField, 'like', "%{$searchValueArr}%"];
            } else {
                $map[] = [$searchField, 'in', $searchValueArr];
            }
        }

        if (!empty($progressActivate)) $map[] = ['progress_activate', '=', $progressActivate];
        if (!empty($asin)) $map[] = ['asin', '=', $asin];

        //权限过滤
//        $createUserAuth = CommonService::userAuth();
        $skuAuth = CommonService::skuAuth();
        $model2 = $this->model->where($map);
        if (!empty($skuAuth)){
            $map[] = ['platform_sku','in',$skuAuth];
            $model2->whereIn('platform_sku',$skuAuth);
        }
        $offset = ($page_size - 1) * $limit;
        $list = $this->model->getList($map, [['create_time', 'desc']],"{$offset},{$limit}");
        $count = $model2->count();

        //获取访客信息记录
        if (!empty($list)) {
            $recordIds = array_column($list, 'record_id');
            $VisitorsRecordModel = new VisitorsRecordModel();
            $recordList = $VisitorsRecordModel->whereIn('id', $recordIds)->get()->toArray();
            $recordCol = array_column($recordList, null, 'id');
        }

        foreach ($list as $kk => $val) {
            //处理礼品图片
            $val['gift_image'] = IMG_URL . ($val['gift_image'] ?? '');
            $val['record_list'] = $recordCol[$val['record_id']] ?? [];
            $val['evaluate_star_rating'] = $val['evaluate_star_rating'] . '星';
            if (!empty($val['record_list']['create_time'])) $val['record_list']['create_time'] = date("Y-m-d", strtotime($val['record_list']['create_time']));
            $list[$kk] = $val;
        }

        return array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );

    }

    /**
     * @Notes:标记已处理
     * @Interface processedStatus
     * @return array
     * @author: wuyu
     * @Time: 2023/5/8   12:16
     */
    public function processedStatus()
    {
        $params = request()->all();

        $id = $params['id'] ?? 0;
        $processedStatus = $params['processed_status'] ?? 0;
        if (empty($id) || !isset($params['processed_status'])) {
            return message("参数不存在", false, []);
        }
        $data = ['processed_status' => $processedStatus];
        $this->model->where('id', $id)->update($data);
        $this->model->cacheReset($id, $data, $id);
        return message("操作成功", true, []);
    }


    /**
     * @Notes:批量标记已处理
     * @Interface processedStatusBatch
     * @author: wuyu
     * @Time: 2023/5/8   12:16
     */
    public function processedStatusBatch()
    {
        $params = request()->all();

        $ids = $params['ids'] ?? [];
        $processedStatus = $params['processed_status'] ?? 0;
        if (empty($ids)) {
            return message("参数不存在", false, []);
        }
        $data = ['processed_status' => $processedStatus];
        $this->model->whereIn('id',$ids)->update($data);
        foreach ($ids as $id){
            $this->model->cacheReset($id, $data, $id);
        }
        return message("操作成功", true, []);

    }


}
